package com.zyy.smartcity1129.hospital;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.HospitalBannerInfo;
import com.zyy.smartcity1129.data.HospitalInfo;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: HospitalFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class HospitalFragment extends BaseFragment {
    private SearchView search;
    private Banner banner;
    private TabLayout tab;
    private RecyclerView rvMyOrder;
    List<HospitalInfo.RowsBean> hospitalList = new ArrayList<>();
    List<HospitalBannerInfo.DataBean> hospitalBannerList = new ArrayList<>();
    RvAd<HospitalInfo.RowsBean> rvAd;
    List<HospitalInfo.RowsBean> hospitals = new ArrayList<>();
    public static HospitalInfo.RowsBean hospital;

    @Override
    protected int layId() {
        return R.layout.f_hd;
    }

    @Override
    protected String title() {
        return "门诊预约";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        XCUtli xcUtli = new XCUtli(2);
        xcUtli.then(() -> {
            hospitalList = Http.getInstance().get("/prod-api/api/hospital/hospital/list", null, HospitalInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            hospitalBannerList = Http.getInstance().get("/prod-api/api/hospital/banner/list", null, HospitalBannerInfo.class, 1).getData();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), a -> {
            if (a.get() == 2) {
                List<Object> list = new ArrayList<>();
                for (HospitalBannerInfo.DataBean d : hospitalBannerList) {
                    list.add(d.getImgUrl());
                }
                banner.setAdapter(new BannerAd(list)).addBannerLifecycleObserver(this).setIndicator(new CircleIndicator(getContext())).start();
                initAdapter(hospitalList);
                rvMyOrder.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMyOrder.setAdapter(rvAd);
            }
        });
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                hospitals.clear();
                hospitals.addAll(hospitalList.stream().filter(c->c.getHospitalName().contains(query)).collect(Collectors.toList()));
                rvAd.notifyDataSetChanged();
//                rvMyOrder.setAdapter(rvAd);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void initAdapter(List<HospitalInfo.RowsBean> hospitalList) {
        rvAd = new RvAd<HospitalInfo.RowsBean>(hospitalList, R.layout.rv_base) {
            private void initView(View iv) {
                ig = (ImageView) iv.findViewById(R.id.ig);
                tv1 = (TextView) iv.findViewById(R.id.tv1);
                tv2 = (TextView) iv.findViewById(R.id.tv2);
                tv3 = (TextView) iv.findViewById(R.id.tv3);
                tv4 = (TextView) iv.findViewById(R.id.tv4);
                tv5 = (TextView) iv.findViewById(R.id.tv5);
                tv3.setVisibility(View.GONE);
                tv4.setVisibility(View.GONE);
                tv5.setVisibility(View.GONE);
            }

            private TextView tv5;
            private TextView tv4;
            private TextView tv3;
            private TextView tv2;
            private TextView tv1;
            private ImageView ig;
            @Override
            protected void initR(View vi, HospitalInfo.RowsBean rowsBean, int p) {
                initView(vi);
                tv1.setText(rowsBean.getHospitalName());
                tv2.setText("星级：" + rowsBean.getLevel());
                Glide.with(ig.getContext()).load(Http.base + rowsBean.getImgUrl()).into(ig);
                vi.setOnClickListener(view -> {
                    hospital = rowsBean;
                    add(new Hospital1Fragment());
                });
            }
        };
    }

    private void initView(View iv) {
        search = (SearchView) iv.findViewById(R.id.search);
        banner = (Banner) iv.findViewById(R.id.banner);
        tab = (TabLayout) iv.findViewById(R.id.tab);
        rvMyOrder = (RecyclerView) iv.findViewById(R.id.rvMyOrder);
    }
}
